'''
Created on 13/06/2012

@author: zebastian
'''
import os
import glob
from numpy import *
from scipy import *
from pylab import *

import numpy as np
import matplotlib.pyplot as plt
'''
PLOTTING LIBRARY
http://matplotlib.sourceforge.net/index.html
'''

def getFscore(lines):
    '''
    File Structure
        runid,recall,precision,f-score
    '''
    #The first line contain headers
    currentLine=1
    f_value=zeros(len(lines)-1)
    for line in lines:
        if currentLine>1:
            #Onlye get the last column where the f-score is located
            f_value[currentLine-2]=line.split(",")[3]
        currentLine=currentLine+1
    return f_value
def getFileName(Archrcc):
    #Getting the file NAME
    split_u=Archrcc.name.split("\\")
    file_n_u=split_u[len(split_u)-1].split(".")
    file_name_u=file_n_u[0]
    return file_name_u
    #
def versusPlot(data1_r2,data1_su,data2_r2,data2_su,file1,file2):
    m1=[mean(data1_r2),mean(data1_su)]
    std1=[std(data1_r2),std(data1_su)]
    m2=[mean(data2_r2),mean(data2_su)]
    std2=[std(data2_r2),std(data2_su)]
    ind=2
    width = 0.35
    p1 = plt.bar(ind, m1,   width, color='r', yerr=std2)
    p2 = plt.bar(ind, m2, width, color='y',bottom=m1, yerr=std1)
    plt.ylabel('F-Score')
    plt.title('F-Score by ROUGE Measure')
    plt.xticks(ind+width/2., ('Rouge 2', 'Rouge SU-4') )
    max_val=max([max(data1_r2),max(data1_su),max(data2_r2),max(data2_su)])+max([std1,std2])
    plt.yticks(np.arange(0,max_val,0.005))
    plt.legend( (p1[0], p2[0]), (file1, file2) )
def generalPlot(data1_r2,data1_su):
    subplot(311)
    #plt.hist(data1_r2,len(data1_r2)/2,normed=True,color="green")
    plt.hist(data1_r2, 70, facecolor='green', alpha=0.75)
    plt.xlabel('F-Score value')
    plt.ylabel('Frequency')
    mean_r2=mean(data1_r2)
    std_r2=std(data1_r2)
    plt.title('ROUGE-2\nMean='+str(mean_r2)+' / STD='+str(std_r2))
    plt.axis([min(data1_r2), max(data1_r2), 0, 40])
    plt.grid(True)
    #
    subplot(313)
    #plt.hist(data1_su, 70, normed=1, facecolor='green', alpha=0.75)
    plt.hist(data1_su, 70,cumulative=False, facecolor='green', alpha=0.75)
    plt.xlabel('F-Score value')
    plt.ylabel('Frequency')
    mean_su=mean(data1_su)
    std_su=std(data1_su)
    plt.title('ROUGE-SU4\nMean='+str(mean_su)+' / STD='+str(std_su))
    plt.axis([min(data1_su), max(data1_su), 0, 50])
    plt.grid(True)
    plt.show()
def processRouge(path,option,f1,f2):
    if option=="all":
        fCr2=0
        fCsu=0
        for infile in glob.glob( os.path.join(path, '*.csv') ):
            Archrcc=open(infile,'r')
            lines=Archrcc.readlines()
            Archrcc.close()
            if infile.find('ROUGE-2.csv')>-1:
                    data_r2=getFscore(lines)
                    fCr2=fCr2+1
            if infile.find('ROUGE-SU4.csv')>-1:
                    data_su=getFscore(lines)
                    fCsu=fCsu+1
            if fCr2==fCsu and infile.find('ROUGE-1.csv')==-1:
                if fCr2==1 and fCsu==1:
                    concatData_r2=data_r2
                    concatData_su=data_su
                if fCr2>1: 
                    concatData_r2=concatenate((concatData_r2,data_r2),0)
                    concatData_su=concatenate((concatData_su,data_su),0)
        generalPlot(concatData_r2,concatData_su)
        #"all"
    else:            
    #Versus
        #for infile in glob.glob( os.path.join(path, '*ROUGE-2.csv') ):
        for infile in glob.glob( os.path.join(path, '*.csv') ):
            if infile.find(f1)>-1:
                Archrcc=open(infile,'r')
                lines=Archrcc.readlines()
                Archrcc.close()
                if infile.find('ROUGE-2.csv')>-1:
                    data1_r2=getFscore(lines)
                if infile.find('ROUGE-SU4.csv')>-1:
                    data1_su=getFscore(lines)
                file1=getFileName(Archrcc)
            if infile.find(f2)>-1:
                Archrcc=open(infile,'r')
                lines=Archrcc.readlines()
                Archrcc.close()
                if infile.find('ROUGE-2.csv')>-1:
                    data2_r2=getFscore(lines)
                if infile.find('ROUGE-SU4.csv')>-1:
                    data2_su=getFscore(lines)
                file2=getFileName(Archrcc)
        versusPlot(data1_r2,data1_su,data2_r2,data2_su,file1,file2)

def main():
    print "-----------Procesando ROUGE's-----------"
    path="C:\FORMAT_CSV"
    processRouge(path, "all", 0, 0)
if __name__ == '__main__':
    main()
    pass